﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CustomerEmailer.Common;
using System.Xml;
using System.Xml.Linq;
using CustomerEmailer.Common;
using System.Runtime.InteropServices;

namespace CustomerEmailer
{
    public partial class Settings : Form
    {
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd,
                         int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();
        Main MainForm;
        public Settings(Main mainForm)
        {
            InitializeComponent();
            MainForm = mainForm;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SaveSetting("UserName", cbUsername.SelectedItem.ToString());
                SaveSetting("Email", tbUser.Text);
                SaveSetting("Password", tbPassword.Text);
                SaveSetting("ServiceName", ((Int32)getSelectedService()).ToString());
                SaveSetting("WaitBetweenSendings", tbWaitBetweenSends.Text);
                SaveSetting("LogFile", tbLogFileLocation.Text );
                SaveSetting("ErrorFile", tbErrorFileLocation.Text);
                SaveSetting("ServerIP", tbServerIP.Text);

                MessageBox.Show("הנתונים נשמרו בהצלחה");
                if (!MainForm.SyncTimer.Enabled)
                {
                    MainForm.SyncTimer.Enabled =true;
                    MainForm.SyncTimer.Start();
                    MainForm.lbSync.Text = "מסתנכרן...";
                }
                this.Close();
            }
            catch (System.UnauthorizedAccessException notAdminException)
            {
                MessageBox.Show("אנא הרץ את האפליקציה כמנהל");
            }
        }

        private void SaveSetting(string settingKey, string settingValue)
        {
                string fileLocation = Utils.GetXmlFileLocation("settings.xml");
                XDocument xDoc = XDocument.Load(fileLocation);
                xDoc.Element("configuration").Element(settingKey).Value = settingValue;
                xDoc.Save(fileLocation);
        }


      

        private void Settings_Load(object sender, EventArgs e)
        {
            cbUsername.DataSource = Utils.GetSetting("UserNamesL").Split('#');
            for (int i = 0; i < cbUsername.Items.Count; i++)
            {
                if (cbUsername.Items[i].ToString() == Utils.GetSetting("UserName").ToString())
                    cbUsername.SelectedIndex = i;
            }
                //tbUsername.Text = Utils.GetSetting("UserName");
                tbUser.Text = Utils.GetSetting("Email");
            tbPassword.Text = Utils.GetSetting("Password");
            EmailService.EmailServiceName serviceName = (EmailService.EmailServiceName)int.Parse(Utils.GetSetting("ServiceName"));
            setSelectedService(serviceName);
            tbWaitBetweenSends.Text = Utils.GetSetting("WaitBetweenSendings");
            tbLogFileLocation.Text =  Utils.GetSetting("LogFile");
            tbErrorFileLocation.Text = Utils.GetSetting("ErrorFile");
            tbErrorFileLocation.Text = Utils.GetSetting("ErrorFile");
            tbServerIP.Text = Utils.GetSetting("ServerIP");
        }

        private EmailService.EmailServiceName getSelectedService()
        {
            EmailService.EmailServiceName selectedService = EmailService.EmailServiceName.Gmail;
            if (gmailRadio.Checked)
                selectedService = EmailService.EmailServiceName.Gmail;
            if (HotmailRadio.Checked)
                selectedService = EmailService.EmailServiceName.Hotmail;
            return selectedService;
        }

        private void setSelectedService(EmailService.EmailServiceName selectedService)
        {
            switch (selectedService)
            {
                case EmailService.EmailServiceName.Gmail:
                    gmailRadio.Checked = true;
                    break;
                case EmailService.EmailServiceName.Hotmail:
                    HotmailRadio.Checked = true;
                    break;
            }
        }

        private void Settings_Paint(object sender, PaintEventArgs e)
        {
            this.Region = new System.Drawing.Region(Utils.drawRoundedRect(0, 0, 15, this.Height, this.Width));
            Utils.drawBorder(0, 0, 15, this.Height, this.Width, e);
        }

        private void Settings_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("IExplore", " http://www.socialbiz.co.il");
        }

        private void cbUsername_SelectedIndexChanged(object sender, EventArgs e)
        {
            tbUser.Text = Utils.GetSetting("EmailL").Split('#')[cbUsername.SelectedIndex];
            tbPassword.Text = Utils.GetSetting("PasswordL").Split('#')[cbUsername.SelectedIndex];
            EmailService.EmailServiceName serviceName = (EmailService.EmailServiceName)int.Parse(Utils.GetSetting("ServiceNameL").Split('#')[cbUsername.SelectedIndex]);
            setSelectedService(serviceName);
        }
    }
}
